package com.mathdroid.gui.main;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import com.mathdroid.exceptionhandling.DroidClipboard;
import com.mathdroid.gui.ImageLoader;
import com.mathdroid.model.simulation.SimulationStatistics;

/**
 * This panel shows statistics about a completed series of simulations.
 */
public final class StatisticsPanel extends JPanel {

	/**
	 * Default UID.
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The horizontal weight of the components.
	 */
	private static final double HORIZONTAL_WEIGHT = 10;
	
	/**
	 * The vertical weight of the scroll pane.
	 */
	private static final double HUGE_VERTICAL_WEIGHT = 100;
	
	/**
	 * The constraints for this UI components.
	 */
	private final GridBagConstraints c = new GridBagConstraints();
	
	/**
	 * The scroll pane for the table.
	 */
	private final JScrollPane sp = new JScrollPane();
	
	/**
	 * The table for the statistics summary.
	 */
	private JTable summary = null;
	
	/**
	 * The simulation statistics of the last simulation series.
	 */
	private SimulationStatistics statistics = null;
	
	/**
	 * Button to copy the content to the clipboard.
	 */
	private final JButton copyToClipboard = new JButton("Copy to Clipboard");
	
	/**
	 * Image loader.
	 */
	private final ImageLoader il = new ImageLoader();
	
	/**
	 * Button to close the frame.
	 */
	private final JButton close = new JButton("Close");
	
	/**
	 * Parent JFrame.
	 */
	private JFrame parent = null;
	
	/**
	 * Default Constructor.
	 * @param p The parent JFrame.
	 */
	public StatisticsPanel(final JFrame p) {
		this.parent = p;
		this.setLayout(new GridBagLayout());
		this.buildScrollPane();
		this.buildCopyButton();
		this.buildCloseButton();
	}

	/**
	 * Build the button to close the frame.
	 */
	private void buildCloseButton() {
		this.c.fill = GridBagConstraints.HORIZONTAL;
		this.c.gridx = 1;
		this.c.gridy = 1;
		this.c.weightx = HORIZONTAL_WEIGHT;
		this.c.weighty = 0;
		this.c.gridwidth = 1;
		
		this.close.setIcon(this.il.getImage("cross"));
		
		this.close.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				parent.dispose();
			}
		});
		
		this.add(this.close, this.c);
	}

	/**
	 * Build the button to copy the content to the clipboard.
	 */
	private void buildCopyButton() {
		this.c.fill = GridBagConstraints.HORIZONTAL;
		this.c.gridx = 0;
		this.c.gridy = 1;
		this.c.weightx = HORIZONTAL_WEIGHT;
		this.c.weighty = 0;
		this.c.gridwidth = 1;
		
		this.copyToClipboard.setIcon(this.il.getImage("clipboard_sign"));
		
		this.copyToClipboard.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				new DroidClipboard().copy(statistics.toString());
			}
		});
		
		this.add(this.copyToClipboard, this.c);
	}

	/**
	 * Build the scroll pane for the table.
	 */
	private void buildScrollPane() {
		this.c.fill = GridBagConstraints.BOTH;
		this.c.gridx = 0;
		this.c.gridy = 0;
		this.c.weightx = HORIZONTAL_WEIGHT;
		this.c.weighty = HUGE_VERTICAL_WEIGHT;
		this.c.gridwidth = 2;
		
		this.add(this.sp, this.c);
	}
	
	/**
	 * Set the simulation statistics.
	 * @param s Simulation statistics.
	 */
	public void setSimulationStatistics(final SimulationStatistics s) {
		this.statistics = s;
		final int size = s.size();
		int i = 0;
		
		String[] columnNames = {
				"Ability", 
				"Direct Damage", "Periodic Damage",
				"Direct Healing", "Periodic Healing", 
				"Hits", "Crits", "Misses", "Costs", "Uses"};
		
		String[][] data = new String[size][columnNames.length];
		
		for (String ability : s.keySet()) {
			data[i][0] = ability;
			data[i][1] = "" + s.get(ability).getDirectDamage();
			data[i][2] = "" + s.get(ability).getPeriodicDamage();
			data[i][3] = "" + s.get(ability).getDirectHealing();
			data[i][4] = "" + s.get(ability).getPeriodicHealing();
			data[i][5] = "" + s.get(ability).getHits();
			data[i][6] = "" + s.get(ability).getCrits();
			data[i][7] = "" + s.get(ability).getMisses();
			data[i][8] = "" + s.get(ability).getCosts();
			data[i][9] = "" + s.get(ability).getUses();
			i++;
		}
		
		this.summary = new JTable(data, columnNames);
		this.summary.setAutoCreateRowSorter(true);
		
		this.sp.setViewportView(this.summary);
	}

}
